﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="email_wbyj_tree.aspx.cs" Inherits="Ccit.OA.Email.email_wbyj_tree" %>

<%@ Register Assembly="Microsoft.Web.UI.WebControls, Version=1.0.2.226, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="Microsoft.Web.UI.WebControls" TagPrefix="iewc" %>
<%@ Register TagPrefix="ccit" Namespace="Ccit.ServerControls" Assembly="Ccit.ServerControls.ActionForm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>外部邮件树</title>
       <script language="javascript" src="../script/jquery.js" type="text/javascript"></script>
    <script language="javascript" src="../script/jbase64.js" type="text/javascript"></script>
    <script language="javascript" src="../script/jsonlib.js" type="text/javascript" ></script>
    <script language="javascript" src="../script/StringClass_Ext.js" type="text/javascript" ></script>
    <style>
		.skin { BORDER-RIGHT: buttonhighlight 2px outset; BORDER-TOP: buttonhighlight 2px outset; FONT-SIZE: 9pt; VISIBILITY: hidden; BORDER-LEFT: buttonhighlight 2px outset; WIDTH: 100px; CURSOR: default; BORDER-BOTTOM: buttonhighlight 2px outset; FONT-FAMILY: Arial, Helvetica, sans-serif; POSITION: absolute; BACKGROUND-COLOR: menu; TEXT-ALIGN: left }
		.menuitems { PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-TOP: 5px; PADDING-BOTTOM: 5px;}
	</style>
</head>
<body onclick="hideMenu()">
    <ccit:ActionForm id="form1" runat="server" >
    <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server" />
    
        <input type="hidden" id="_hidUpAddrId" value="0_0" runat="server" />  
    <input type="hidden" id="_hidAddNewEml" runat="server" /> <!-- 新增记录标志 -->
    <input type="hidden" id="_hidDelEmlId" runat="server" />  
    <input type="hidden" id="_hidNewEmlObject" runat="server" />  <!--新增签名消息对象-->
    
                    <div id="root" class="skin" style="LEFT:0px; VISIBILITY:hidden; WIDTH:110px; TOP:0px; HEIGHT:35px">
        <div  onclick="clickItem()" onMouseover="highlighItem()" onMouseout="lowlightItem()" style="BORDER-BOTTOM: window 2px inset;" class="menuitems" func="TestAccount">            
                <img src="../images/toolbar/xxsbmit.gif" border="0" />&nbsp; 测试连接            
        </div>
        <div onclick="clickItem()" onMouseover="highlighItem()" onMouseout="lowlightItem()" style="BORDER-BOTTOM: window 2px inset;" class="menuitems" func="RecEmail">
            <img src="../images/toolbar/dirsend.gif" border="0" />&nbsp; 接收邮件
        </div>
         <div onclick="clickItem()" onMouseover="highlighItem()" onMouseout="lowlightItem()" class="menuitems" func="DeleteAccount">
            <img src="../images/toolbar/del_bq.gif" border="0" />&nbsp; 删除账号
        </div>
    </div>
        
    <table cellspacing="0" cellpadding="0" align="right" border="0" width="100%" height="100%" style="background-color:#EEF2F7; border:0px solid #999999">
			<tr height="100%" valign="top">
			<td id="Td1"  runat="server">
                <iewc:TreeView ID="trvAccount" runat="server"></iewc:TreeView>
    		</td>
		    </tr>
		    <tr runat="server" id="trSet" style="display:none">
		        <td align="center">
		            &nbsp;<asp:Button ID="btGetValue" runat="server" Text="确 定" CssClass="button" 
                        onclick="btGetValue_Click" />
		        </td>
		    </tr>
	</table>
 <input type="button" id="btnTrigger" onclick="doTrigger()" style="display:none" value="触发事件" />
    <input type="button" id="btnDelTrigger" onclick="doDelTrigger()" style="display:none" value="删除触发事件" />
    </ccit:ActionForm>
    <script type="text/javascript">
    <!--
        var menuskin = "skin";
		var node = null;
		var menuname = "root";		
		
		/* 用于更新变更前后节点,定义的全局变量 */
		var ctrlOgNode = null;
		var ctrlDtNode = null;
		var vOgNodeIndex;
		var vDtNodeIndex;
		
		function hideMenu() 
		{		    
			var obj = document.getElementById(menuname);
			if (null != obj && typeof(obj) != "undefined")
				document.getElementById(menuname).style.visibility = "hidden";
		}
		
		function highlighItem() 
		{
			if (event.srcElement.className == "menuitems") 
			{
				event.srcElement.style.backgroundColor = "highlight";
				event.srcElement.style.color = "white";
			}
		}
		function lowlightItem() 
		{
			if (event.srcElement.className == "menuitems") 
			{
				event.srcElement.style.backgroundColor = "";
				event.srcElement.style.color = "black";
				window.status = "";
			}
		}
		function clickItem() 
		{								
			if(node != null)
			{
				var vNodeId = node.getAttribute("ID");
				//alert(vNodeId);				
				var nodeType = node.getAttribute("type").toUpperCase();
				var vNodeIdArray = vNodeId.split("_");
				var vMenuId = vNodeIdArray[0];
												
				if (event.srcElement.className == "menuitems" && event.srcElement.disabled != true) 
				{
					var sFuncName = event.srcElement.getAttribute("func");					
					switch (sFuncName)
					{
						case "TestAccount":	//测试邮箱账号
						    //var vPageParam = "Id=" + vMenuId;
	                        var jsonstr = jsonObj();                        
                            PageMethods.TestAccount(jsonstr,ClientCallBack);						    				
							break;
						case "RecEmail":    //接收外部邮件
						    var jsonstr = jsonObj();
						    PageMethods.ReciveEmail(jsonstr,ClientCallBack);
						    break;
						case "DeleteAccount": //删除邮箱账号						    
						    if(!confirm("删除当前外部邮箱账号,是否继续?"))
                                return;
                            var jsonstr = jsonObj();                        
                            PageMethods.DeleteAccount(jsonstr,ClientCallBack);						    
							break;
						default:
							break;
					}						
				}
			}
		}
		
		function trvAccount.onexpand()
		{	
		    /* Step 1：找到当前点击的TreeNode */	    
		    var nodeIndex = event.treeNodeIndex;			    	
			if (typeof(nodeIndex) == "undefined"){			
				node = null;
				return false;
			}
			
			trvAccount.selectedNodeIndex = nodeIndex;
			node = trvAccount.getTreeNode(nodeIndex);
						
			/* Step 2: 获取菜单ID，获取菜单下的子菜单 */			
			var vNodeId = node.getAttribute("ID");
			var vNodeIdArray = vNodeId.split("_")
			var vMenuId = vNodeIdArray[0];
			var vLoadSign = vNodeIdArray[1];
						
			if(vMenuId != "0" && vLoadSign == "0") {
			    var oSubNodes = node.getChildren();
			    var oSubNode;
			    for(var i = oSubNodes.length - 1; i >= 0; i --) {
			        oSubNode = oSubNodes[i];
			        oSubNode.remove();
			    }
			    
			    var expandSign = $get("_hidExpandSign").value;//是否逐级展开 1：是
			    var levelStruct = $get("_hidLevelStruct").value;		    			    
			    PageMethods.GetSubMenusByMenuId(vMenuId,nodeIndex,expandSign,levelStruct,ClientCallBack);
			    
			}
			hideMenu();
		}
		
		function trvAccount.oncollapse()
		{
			hideMenu();
		}
		
		function trvAccount.oncontextmenu()
		{				
			hideMenu();
			var nodeIndex = event.treeNodeIndex;							
			if (typeof(nodeIndex) == "undefined")
			{
				node = null;
				return false;
			}
			trvAccount.selectedNodeIndex = nodeIndex;
			node = trvAccount.getTreeNode(nodeIndex);			
			var vNodeId = node.getAttribute("ID");			
			var vNodeIdArray = vNodeId.split("_");
			var vMenuId = vNodeIdArray[0];
			var vLay = vNodeIdArray[2];
			
			if(vMenuId == "0") return;  //根节点不显示右键菜单
			
			var shortCutMenu = $get("root");
			if(vLay == "3") {			    
			    shortCutMenu.children(0).disabled = "disabled";
			}
			else {
			    shortCutMenu.children(0).disabled = "";
			}
			
			
			var obj = document.getElementById("root");				
			if (null == obj || typeof(obj) == "undefined")
			{
				node = null;
				return false;
			}
			
			var rightedge = document.body.clientWidth - event.clientX;		//右边距

			var bottomedge = document.body.clientHeight - event.clientY;	//底边距距距
			
			if (rightedge < obj.offsetWidth)
				obj.style.left = document.body.scrollLeft + 5 + event.clientX - obj.offsetWidth;
			else
				obj.style.left = document.body.scrollLeft + 5 + event.clientX ;
			
			if (bottomedge < obj.offsetHeight)
				obj.style.top = document.body.scrollTop + 5 + event.clientY - obj.offsetHeight ;
			else
				obj.style.top = document.body.scrollTop + 5 + event.clientY ;
											
			obj.style.visibility = "visible";
			return false;
		}	
        
        
        function findTreeNode(pMenuId) {
            var ctrlTree = document.getElementById("trvAccount");		    
		    
		    var ctrlRoot = ctrlTree.getTreeNode("0"); 
		    var ctrlChildNodes = ctrlRoot.getChildren();
		    
		    var ctrlFindNode = recursionFind(ctrlChildNodes,pMenuId);
		    
		    return ctrlFindNode;
        }
        
        function recursionFind(ctrlChildNodes,pMenuId) {
            var iLen = ctrlChildNodes.length;
            var ctrlTreeNode;
		    var vNodeId;
		    var vNodeIdArray;
		    var vDeptId;
		    var ctrlSubChildNodes;
		    var bFind = false;
		    
            for(var i = 0; i < iLen; i ++) {
		        ctrlTreeNode = ctrlChildNodes[i];
		        vNodeId = ctrlTreeNode.getAttribute("ID");    
		        vNodeIdArray = vNodeId.split("_")
		        vMenuId = vNodeIdArray[0];
		        if(vMenuId == pMenuId) {		           
		            bFind = true;
		            break;       
		        }
		        else {
		            ctrlSubChildNodes = ctrlTreeNode.getChildren();
		            ctrlTreeNode = recursionFind(ctrlSubChildNodes,pMenuId);
		            if(null != ctrlTreeNode) {
		                bFind = true;
		                break;
		            }   
		        }
		    }
            
            if(bFind)
                return ctrlTreeNode;
            else
                return null;
        }
        
        function doTrigger() {
            doOperOriParentNode();  // update origin parent node
		}
		
		function doOperOriParentNode() {	
		    var NodeExpr = $get("_hidNewEmlObject").value;   //获取的是当前编辑的节点对象 eg:{"FId":"26","FName":"测试类型","Superiorid":"1"}
		    eval("var NodeObj=" + NodeExpr);	    //获取节点消息对象	
		    var isAddNew = $get("_hidAddNewEml").value;
		    
		    if(isAddNew=="1"){//编辑
		        // find current node position index
		        var curNodeId = NodeObj.Id;
                var curNode = findTreeNode(curNodeId);
                
		        if(curNode != null) {  //parent node no change
		            curNode.setAttribute("Text",NodeObj.Title);
		            return;
		        }
		    }
		    else {
		        createSubTNodes("0",NodeObj);
		    }
		     
		}

	    function expandDtNode(dtNodeId) {
		    $get("_hidExpandSign").value = "1";//连续展开标志
		    PageMethods.GetNodeLevelStruct(dtNodeId,ClientCallBack);
		}
		
		function doDelTrigger() {
		    var delMenuId = $get("_hidDelEmlId").value;
		    ctrlNode = findTreeNode(delMenuId);
		    nodeIndex = ctrlNode.getNodeIndex();
		    var node = $get("trvAccount").getTreeNode(nodeIndex); 
            node.remove();            
		}
		
		function expandParentNode(dtNode) {
		    var dtParentNode = dtNode.getParent();
			dtParentNode.setAttribute("EXPANDED",true);
			
			var dtNodeId = dtParentNode.getAttribute("ID");
			var vNodeIdArray = dtNodeId.split("_")
			var vDeptId = vNodeIdArray[0];
			if (vDeptId != "0") {			    
			    expandParentNode(dtParentNode);
			}
			
		}
		
		function initialTreeNode(ctrlFindNode) {
		    var ctrlTree = document.getElementById("trvAccount");	
		    var vNodeId;
		    var vNodeIdArray;
		    var vMenuId;	
		    var vNodeIndex;
		    if(null != ctrlFindNode) {
		        vNodeId = ctrlFindNode.getAttribute("ID");    
		        vNodeIdArray = vNodeId.split("_")
		        vMenuId = vNodeIdArray[0];
		        ctrlFindNode.setAttribute("ID",vMenuId + "_" + "0");
		        vNodeIndex = ctrlFindNode.getNodeIndex();
		        ctrlTree.selectedNodeIndex = vNodeIndex;
		        /* 如果当前节点没有下级节点，则不会触发expand事件,
		         * 此时需要在当前节点下增加一个空节点 */
		        var vChildNodes = ctrlFindNode.getChildren();
		        if(vChildNodes.length == 0) {
		           var oEmptyNode = $get("trvAccount").createTreeNode();  
		           ctrlFindNode.add(oEmptyNode);
		        }
		        ctrlFindNode.setAttribute("EXPANDED",true);
		    }
		}
		
				        //追加子节点
        function createSubTNodes(nodeIndex,oResult) { 
		     var sParam1;
		     var encodeParam1;
	   
		    sfiesId = oResult.Id;
	         
		     var node = $get("trvAccount").getTreeNode(nodeIndex);
         	         sParam1 = "XH=" + sfiesId;
                     var encodeParam1 = base64encode(sParam1);
                     vNavigateUrl = "./email_wbyj_edit.aspx?"+encodeParam1;
                     oSubNode = $get("trvAccount").createTreeNode();
                     oSubNode.setAttribute("ID",sfiesId);
                     oSubNode.setAttribute("Text",oResult.Title);
                     oSubNode.setAttribute("Type","root");
                     oSubNode.setAttribute("Target","iFrmEdit");
                     oSubNode.setAttribute("ImageUrl","../images/toolbar/bfiles.gif");
                     oSubNode.setAttribute("NavigateUrl",vNavigateUrl);
    		         
    		         
    		        oEmptyNode = $get("trvAccount").createTreeNode();  
                     node.add(oSubNode);

        }
		
		
		function jsonObj() //返回序列化后的字符串
        {

         var MsSigna ={};
                MsSigna.Id = node.getAttribute("ID");
                MsSigna.userId = "";

                var jsonStr = JSON.stringify(MsSigna,function(key,value){return value;});  
                return jsonStr;
        }
        
          function ClientCallBack(vResult) {
            eval("var resultObj=" + vResult);
            var resultSign = resultObj.resultSign;
            var operType = resultObj.operType;
            switch(operType)
            {
                case "Del":{
                    if(resultSign == "1")
                    {
                        $get("_hidDelEmlId").value = resultObj.resultId;
                        doDelTrigger();
                        //alert("删除成功!");
                        parent.frames[1].location.href = "email_wbyj_edit.aspx"
                        //window.location.href="email_wbyj_tree.aspx";
                    }
                    else
                    {
                        alert("删除失败!");
                        return false;
                    }
                    break;
                }
                case "Test":{
                    if(resultSign == "1")
                    {
                        alert("测试外部邮箱连接成功!");
                    }
                    else
                    {
                        alert("测试外部邮箱连接失败!");
                        return false;
                    }
                    break;
                }
                case "Recive":{
                    if(resultSign == "00")
                    {
                        alert("邮件接收成功!");
                    }
                    else
                    {
                        alert("邮件接收失败,请检查邮件设置是否正确!");
                        return false;
                    }
                    break;
                }
                default:
                    return false;
                break;
                
                
            }
            resultObj = null;
    }
    //-->
    </script>
   
</body>
</html>
